import os
import glob
import sys

def variance(moment):
    mean = sum(moment) / len(moment)
    var = 0
    for m in moment:
        var += (m - mean) ** 2
    return var / len(moment)

def smallestDistance(moment):
    sort = sorted(moment)
    dis = 10000
    for i in range(0, len(moment) - 1):
        if abs(moment[i] - moment[i + 1]) < dis:
            dis = abs(moment[i] - moment[i + 1])
    return dis

# path = sys.argv[1]

def humoments(path):
# compute Hu's moments for all files in font
    print "Computing Hu's moments for font in {0}...".format(path)
    hu = []
    for (j, infile) in enumerate(glob.glob(os.path.join(path, '*.png'))):
        hu.append([])
        p = os.popen('./cmoment {0}'.format(infile),"r")
        for i in range(0, 7):
            hu[j].append(float(p.readline().strip()))

# compute relevant statistics
#   stat = []
#   for (i, moment) in enumerate(hu):
#       stat.append([i, max(moment), min(moment), max(moment) - min(moment), sum(moment) / len(moment), variance(moment), smallestDistance(moment)])
#        print "Hu's moment {0}:".format(i)
#        print "Max: {0}".format(max(moment))
#        print "Min: {0}".format(min(moment))
#        print "Range: {0}".format(max(moment) - min(moment))
#        print "Mean: {0}".format(sum(moment) / len(moment))
#        print "Variance: {0}".format(variance(moment))
#        print "Smallest distance: {0}".format(smallestDistance(moment))
    return hu
